package mul.UI;

import mul.Storage.LeaderBoardInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.swarmconnect.Swarm;
import com.swarmconnect.SwarmActiveUser;
import com.swarmconnect.SwarmActivity;
import com.swarmconnect.SwarmLeaderboardScore;
import com.swarmconnect.SwarmUser;
import com.swarmconnect.delegates.SwarmLoginListener;

public class Main extends SwarmActivity {

	/** Called when the activity is first created. */

	private final int APP_ID = 350;
	private final String APP_KEY = "ec4c66318ba0e08384c6101676ea0576";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Swarm.init(this, APP_ID, APP_KEY, mySwarmLoginListener); 
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		Swarm.setAllowGuests(false);
	}

	public void startButton(View view){
		Intent GameIntent = new Intent(Main.this, GameActivity.class); 
		Main.this.startActivity(GameIntent);
	}

	public void highScoreButton(View view){
		Intent intent = new Intent(this, HighScoreActivity.class);
		startActivity(intent);
	}

	public void settingsOnClick(View v){
		Intent myIntent = new Intent(Main.this, SettingsActivity.class); 
		Main.this.startActivity(myIntent);
	}

	private SwarmLoginListener mySwarmLoginListener = new SwarmLoginListener() {
		// This method is called when the login process has started
		// (when a login dialog is displayed to the user).
		public void loginStarted() {
		}

		// This method is called if the user cancels the login process.
		public void loginCanceled() {
		}

		// This method is called when the user has successfully logged in.
		public void userLoggedIn(SwarmActiveUser user) {
			//POST ONLINE from offline
			SwarmLeaderboardScore localUser = LeaderBoardInterface.readUserScoreFromLocal(Main.this);

			if(LeaderBoardInterface.isNetworkAvailable(Main.this)){
				//POST to LOCAL from online
				SwarmLeaderboardScore score = new SwarmLeaderboardScore();
				SwarmUser tempUser = new SwarmUser();
				tempUser.userId = user.userId;
				tempUser.username = user.username;
				tempUser.points = user.points;

				score.score = 0;
				score.user = tempUser;

				LeaderBoardInterface.writesUserScoreToLocal(Main.this, score);
				Log.i("help", "UserScore osted to local from online:  "+score.user.username);

				if(!(localUser == null)){
					LeaderBoardInterface.submitScore(Main.this, localUser.score);
					Log.i("help", "Score submittet online from local:  "+localUser.score);
				}
			}
		}

		// This method is called when the user logs out.
		public void userLoggedOut() {
		}
	};
}